using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "SpriteAnimLibrary", menuName = "Sprite/AnimLibrary")]
public class SpriteAnimLibrary : ScriptableObject
{
    [Tooltip("所有动画片段的列表")]
    public List<SpriteAnimData> animClips = new List<SpriteAnimData>();

    // 在运行时的字典缓存，用来按 animKey 快速查找动画
    private Dictionary<string, SpriteAnimData> _clipDict;

    // 初始化缓存字典
    public void Initialize()
    {
        _clipDict = new Dictionary<string, SpriteAnimData>();
        foreach (var clip in animClips)
        {
            if (!string.IsNullOrEmpty(clip.animKey))
            {
                _clipDict[clip.animKey] = clip;
            }
        }
    }

    // 获取指定 animKey 对应的动画数据
    public SpriteAnimData GetClip(string animKey)
    {
        if (_clipDict == null)
        {
            Initialize();
        }

        if (_clipDict.TryGetValue(animKey, out var data))
        {
            return data;
        }

        Debug.LogWarning($"[SpriteAnimLibrary] 未找到动画：{animKey}");
        return null;
    }
}